package com.boe.common.model;


import com.boe.common.util.freemark.mvc.FreemarkerHas;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;


@Data
@NoArgsConstructor
public class DbTableCodeTemplateData extends BaseFreemarkerModel {
    private String pkg;
    private String group;

    private String entityName;
    private String entityCn;


    private String tableName;

    private List<TableColumnDetail> columns;

    @Setter(AccessLevel.NONE)
    private FreemarkerHas has = new FreemarkerHas();


    public  List<TableColumnDetail> getEntityColumns(){
        var baseEntityName = getBaseEntityName();
        if( baseEntityName.equals("BaseObjectEntity")){
            return this.columns.stream().filter(it->!it.columnName.equals("id")).collect(Collectors.toList());
        }
        else if( baseEntityName.equals("BaseEntity")){
            return this.columns.stream().filter(it->
                    !it.columnName.equals("id") &&
                    !it.columnName.equals("create_by") &&
                    !it.columnName.equals("update_by") &&
                    !it.columnName.equals("create_at") &&
                    !it.columnName.equals("update_at") &&
                    !it.columnName.equals("is_deleted")
            ).collect(Collectors.toList());
        }

        return this.columns;
    }

    public String getBaseEntityName() {
        if (this.columns == null) {
            return "";
        }

        var columnNames = this.columns.stream().map(it -> it.columnName).collect(Collectors.toList());

        if (columnNames.contains("id") == false) {
            return "BaseObjectEntity";
        }

        if (
                columnNames.contains("create_by") &&
                        columnNames.contains("update_by") &&
                        columnNames.contains("create_at") &&
                        columnNames.contains("update_at") &&
                        columnNames.contains("is_deleted") &&
                        columnNames.contains("id")
        ) {
            return "BaseEntity";
        }

        return "BaseIdEntity";
    }


    public DbTableCodeTemplateData(String pkg, String group) {
        this.pkg = pkg;
        this.group = group;
    }

}
